#include "t0022-0.h"
#include "cui.h"

#define MODNAME     "[COPS]"

static int cui_dummy_initialize(struct cui_widget_st *widget)
{
    return 0;
}

static int cui_dummy_parse(cJSON *root, struct cui_widget_st* widget)
{
    return 0;
}

static int cui_dummy_create(struct cui_widget_st *widget)
{
    return 0;
}

static void cui_dummy_paint(struct cui_widget_st *widget)
{
    ERRSYS_INFOPRINT("[%s] dummy paint\n", widget->name);
    return;
}

static void cui_dummy_destroy(struct cui_widget_st *widget)
{
    return;
}

static int cui_dummy_keymouseevent(struct cui_widget_st* widget, int c)
{
    // not handled by default
    ERRSYS_INFOPRINT("cui_dummy_keymouseevent %p\n", cui_dummy_keymouseevent);
    return CUI_WIDGETEVENT_PROPAGATION;
}

// static int dummy_initialize(struct cui_widget_st* widget)
// {
//     cui_dump_widget_hierarchy(widget);
//     ERRSYS_INFOPRINT("dummy initializer\n");
//     return 0;
// }

void cui_default_operation_handler(struct cui_widget_operations *ops)
{
    if (ops->initialize == NULL) {
        ops->initialize = cui_dummy_initialize;
    }
    if (ops->parse == NULL) {
        ops->parse = cui_dummy_parse;
    }
    if (ops->create == NULL) {
        ops->create = cui_dummy_create;
    }
    if (ops->paint == NULL) {
        ops->paint = cui_dummy_paint;
    }
    if (ops->destroy == NULL) {
        ops->destroy = cui_dummy_destroy;
    }

    if (ops->keyevent == NULL) {
        ops->keyevent = cui_dummy_keymouseevent;
    }
}

LIST_HEAD(widgetopslist);

extern struct cui_widget_operations __window_ops;
extern struct cui_widget_operations __menu_ops;
extern struct cui_widget_operations __panel_ops;
extern struct cui_widget_operations __form_ops;
struct cui_widget_operations __dummy_ops;

void cui_initialize_operation(void)
{
    register_widget_operation(&__window_ops);
    register_widget_operation(&__menu_ops);
    register_widget_operation(&__panel_ops);
    register_widget_operation(&__form_ops);
    register_widget_operation(&__dummy_ops);
}

int register_widget_operation(struct cui_widget_operations *ops)
{
    if (!list_empty(&widgetopslist)) {
        struct list_head *pos = NULL;
        list_for_each(pos, &widgetopslist) {
            struct cui_widget_operations *widgetops = list_entry(pos, struct cui_widget_operations, node);
            if (is_string_equal_safe(ops->name, widgetops->name)) {
                return -1;
            }
        }
    }
    INIT_LIST_HEAD(&ops->node);
    list_add(&ops->node, &widgetopslist);
    cui_default_operation_handler(ops);
    return 0;
}

struct cui_widget_operations* get_widget_operation(const char *name)
{
    if (!list_empty(&widgetopslist)) {
        struct list_head *pos = NULL;
        list_for_each(pos, &widgetopslist) {
            struct cui_widget_operations *widgetops = list_entry(pos, struct cui_widget_operations, node);
            if (is_string_equal_safe(name, widgetops->name)) {
                return widgetops;
            }
        }
    }
    return NULL;
}

struct cui_widget_operations __dummy_ops = {
    .name = "dummy",
    .parse = cui_dummy_parse,
    .create = cui_dummy_create,
    .paint = cui_dummy_paint,
    .destroy = cui_dummy_destroy,
};